package com.cn.crmsystem.service.approveService.impl;
import com.cn.crmsystem.mapper.approveMapper.BranchMapper;
import com.cn.crmsystem.pojo.Login;
import com.cn.crmsystem.pojo.ResultJson;
import com.cn.crmsystem.pojo.Task;
import com.cn.crmsystem.pojo.User;
import com.cn.crmsystem.service.approveService.BranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BranchImpl implements BranchService{
	@Autowired
	BranchMapper branchMapper;
//全查
	@Override
	public ResultJson SelectBranchAll(List<User> l) {
		ResultJson resultJson=new ResultJson();
		Integer userid;
		for (int j=0;j<l.size();j++) {
			userid=l.get(j).getId();
			List<Task> list = new ArrayList<>();
			list = branchMapper.SelectBranchAll(userid);
			Integer h;
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					h = list.get(i).getCreate_Id();
					User user = new User();
					user = branchMapper.selectLeader(h);
					if (user == null) {
						break;
					} else {
						list.get(i).setUser(user);
					}
					if (i == list.size() - 1) {
						Map<String, Object> map3 = new HashMap<>();
						map3.put("下属任务全查信息", list);
						resultJson.getSuccess("成功", map3);
					}
				}
			} else {
				resultJson.getSuccess("失败");
			}
		}
		return resultJson;
	}
	//条件查
	@Override
	public ResultJson SelectBranchConditionAll(Task task, Login login) {
		ResultJson resultJson=new ResultJson();
		HashMap<Object,Object> map = new HashMap<>();
		map.put("login",login);
		map.put("task",task);
		List<Task> list=new ArrayList<>();
		Integer role=task.getRole();
		Integer g;
		if(role==0) {
			list=branchMapper.SelectBranchJoinCondition(map);
			if(list!=null) {
				for(int i=0;i<list.size();i++)
				{
					g=list.get(i).getCreate_Id();
					User user=new User();
					user=branchMapper.selectLeader(g);
					if(user==null){
						break;
					}else {
						list.get(i).setUser(user);
					}
					if(i==list.size()-1){
						Map<String,Object> map2=new HashMap<>();
						map2.put("下属任务条件查询信息",list);
						resultJson.getSuccess("成功",map2);
					}
				}
			}else{
				resultJson.getSuccess("失败");
			}
		}else{
			list=branchMapper.SelectBranchChargeCondition(map);
			if(list!=null) {
				Map<String,Object> map1=new HashMap<>();
				map1.put("任务信息",list);
				map1.put("领导人",login);
				resultJson.getSuccess("成功",map1);
			}else{
				resultJson.getSuccess("失败");
			}
		}
		return resultJson;
	}
	//查询参与者
	@Override
	public ResultJson SelectBranchJoinner(Login login) {
		ResultJson resultJson=new ResultJson();
		List<User> list=new ArrayList<>();
		list=branchMapper.SelectUser(login.getUser_id());
		if(list!=null) {
			HashMap<Object,Object> map4 = new HashMap<>();
			map4.put("下属参与者",list);
			resultJson.getSuccess("成功",map4);
		}else{
			resultJson.getSuccess("失败");
		}
		return resultJson;
	}
	//更新下属任务
	@Override
	public ResultJson UpdateBranchTask(Task task) {
		ResultJson resultJson=new ResultJson();
		Integer taskid=task.getTask_Id();
		Integer createid=task.getCreate_Id();
		java.util.Date enddate=task.getEnd_Date();
		String taskname=task.getTaskname();
		String describe=task.getDescribe();
		Integer s=branchMapper.UpdateBranchTask(task);
	    List<User>  list=new ArrayList<>();
		list=task.getList();
		if(s>0){
			resultJson.getSuccess("成功");
		}else{
			resultJson.getFail("失败");
		}
		return resultJson;
	}
    //参与者插入
	@Override
	public ResultJson JoinnerInsert(Task task) {
		ResultJson resultJson=new ResultJson();
		Integer taskid=task.getTask_Id();
		List<User> list=new ArrayList<>();
		list=task.getList();
		Integer userid;
		for(int i=0;i<list.size();i++) {
			userid=list.get(i).getId();
			int l=branchMapper.JoinnerInsert(taskid,userid);
			if(i==list.size()-1&&l>0){
				resultJson.getSuccess("成功k");
			}
		}
		return resultJson;
	}
	//参与者删除
	@Override
	public ResultJson JoinnerDelete(Task task) {
		ResultJson resultJson=new ResultJson();
		Integer taskid=task.getTask_Id();
		List<User> list=new ArrayList<>();
		list=task.getList();
		Integer userid;
		for(int i=0;i<list.size();i++) {
			userid=list.get(i).getId();
			int h=branchMapper.JoinnerDelete(userid,taskid);
			if(i==list.size()-1&&h>0){
				resultJson.getSuccess("成功");
			}
		}
		return resultJson;
	}
//下属任务删除
	@Override
	public ResultJson DeleteBranchTask(Integer taskid) {
		ResultJson resultJson=new ResultJson();
		Integer s1=branchMapper.DeleteBranchTask(taskid);
		if(s1>0){
			resultJson.getSuccess("成功");
		}else{
			resultJson.getFail("失败");
		}
		return resultJson;
	}
	//下属任务增加
	@Override
	public ResultJson InsertBranchTask(Task task) {
		ResultJson resultJson=new ResultJson();
		Integer s2=branchMapper.InsertBranchTask(task);
		if(s2>0){
			resultJson.getSuccess("成功");
		}else{
			resultJson.getFail("失败");
		}
		return resultJson;
	}
//查询所有下属
	@Override
	public List<User> selecteAllauser(Integer userid) {
		List<User> list=new ArrayList<>();
		list=branchMapper.SelectUser(userid);
		return list;
	}
}
